package com.car.loan.framework.security.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author huangding
 * @description
 * @date 2019/3/29 13:40
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CosFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws
        IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers",
            "authorization,Authorization,cache-control,Cache-Control,content-type,expires,pragma,Accept,x-requested-with,"
                + "Origin,Accept,withCredentials, Access-Control-Allow-Headers, X-Requested-By,"
                + "X-Requested-With, Access-Control-Allow-Methods, Access-Control-Allow-Origin,Depth,User-Agent,X-File-Size,"
                + "X-Requested-ByIf-Modified-Since,X-File-Name,X-File-Type");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        filterChain.doFilter(req, res);

    }

    @Override
    public void destroy() {

    }

}
